<?php
namespace app\Backend\controller;;
use think\Controller;
use think\Db;
use think\Input;

class Index extends Common
{
    public function _initialize(){
        parent::_initialize();
    }
    public function index()
    {

        return $this->fetch();
    }

    //递归删除runtime缓存
    public function clear(){
        if ($this->_deleteDir(RUNTIME_PATH)) {
            $result['info'] = '清除缓存成功!';
            $result['status'] = 1;
        } else {
            $result['info'] = '清除缓存失败!';
            $result['status'] = 0;
        }
        $result['url'] = url('admin/index/index');
        return $result;
    }

    private function _deleteDir($path)
    {
        $handle = opendir($path);
        while (($item = readdir($handle)) !== false) {
            if ($item != '.' and $item != '..') {
                if (is_dir($path . '/' . $item)) {
                    $this->_deleteDir($path . '/' . $item);
                } else {
                    if (!unlink($path . '/' . $item))
                        die('error!');
                }
            }
        }
        closedir($handle);
        return rmdir($path);
    }

    //后台用户 修改 自己的密码
    public function editPassword(){
        $user_id =  $this->currentUser['admin_id'];
        if($this->request->isPost()){
            $data = $this->request->post();
            $pass  = db('admin')->where(array('admin_id'=>$user_id))->value('pwd');
            if(empty($data['oldpassword'])){
                $this->error('请输入当前登录密码.');
            }
            if(md5($data['oldpassword']) != $pass){
                $this->error('当前密码输入不对.');
            }
            if(empty($data['password'])){
                $this->error('请输入新的登录密码.');
            }
            if($data['password'] != $data['repassword']){
                $this->error('两次输入密码不一致.');
            }
            if(db('admin')->where(array('admin_id'=>$user_id))->update(array('pwd'=>md5($data['password'])))){
                $this->success('密码已经修改成功.','/reload');
            }
            $this->error('修改密码失败.');
        }
        return view();
    }

    //后台用户 修改 自己的个人信息
    public function users(){
        $userId =  $this->currentUser['admin_id'];
        if($this->request->isPost()){
            $data = $this->request->post();
            if(empty($data['nickname'])){
              $this->error(L('请输入用户昵称.'));
            }
            $data['username'] = trim($_POST['username']);
            if(!preg_match("/^[0-9a-zA-Z]{5,10}$/", $data['username'])){
                $this->error('不合法的登录账号.');
            }
            $map['admin_id'] = array('neq',$userId);
            $map['username'] = $data['username'];
            if(db('admin')->where($map)->count()){
                $this->error("该账号已被占用，请输入其他.");
            }
            if(!empty($data['email']) && !is_email($data['email'])){
               $this->error("输入的邮箱格式不对.");
            }
            // if(!empty($data['mobile']) && !isPhone($data['mobile'])){
            //    $this->error("输入手机号码格式不对.");
            // }
            if(db('admin')->where(array('admin_id'=>$userId))->update($data)){
                $this->success('信息修改成功','/reload');
            }
            echo db('admin')->getLastSql();
            $this->error('信息修改失败.');
        }
        $userInfo = db('admin')->where(array('admin_id'=>$userId))->find();
        if(!$userInfo){
            $this->error('用户不存在.');
        }
        $this->groups = db("auth_group")->select();
        $this->assign('row',$userInfo);
        $this->assign('groups',$this->groups);
        return view();
    }
    
}
